//
//  ClubResetPasswordViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/23.
//

import UIKit

class ClubResetPasswordViewController: ClubBaseViewController, UITextFieldDelegate  {

    private let bgView = c_make_ex(UIImageView.self)
        .image("Login_bg").mode(.scaleAspectFill).end()
    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let logoImageView = c_make_ex(UIImageView.self)
        .image("Login_logo").mode(.scaleAspectFill).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("重置密码").color("#17181A").font(24.font_semiBold).end()
    let oldPasswordTextfield = c_make_ex(UITextField.self)
        .bgColor(.white).corner(25).color(.black).font(14.font_medium)
        .placeholder("请输入6~12位密码").end()
    let newPasswordTextfield = c_make_ex(UITextField.self)
        .bgColor(.white).corner(25).color(.black).font(14.font_medium)
        .placeholder("请输入6~12位密码").end()
    private let sureButton = c_make_ex(UIButton.self)
        .bgImage(UIImage.colorToImage(color: "#FFC99A".toColor), state: .disabled)
        .bgImage(UIImage.colorToImage(color: "#FF831A".toColor))
        .corner(22).color(.white).text("确 定").end()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            bgView
            backButton
            titleLabel
            logoImageView
            oldPasswordTextfield
            newPasswordTextfield
            sureButton
        }
        
        bgView.fillContainer()
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.leading(16).top(.navi_height + 40)
        logoImageView.trailing(0).top(.statusHeight + 4).height(187).width(142)
        oldPasswordTextfield.leading(16).trailing(16).height(50).top(.navi_height + 118.0.Scale)
        newPasswordTextfield.leading(16).trailing(16).height(50)
        newPasswordTextfield.Top == oldPasswordTextfield.Bottom + 24
        sureButton.leading(16).trailing(16).height(44)
        sureButton.Top == newPasswordTextfield.Bottom + 40
        
        oldPasswordTextfield.style { v in
            v.placehold("#7B7D80".toColor, font: 14.font_medium)
            let leftImageV = UIImageView(frame: CGRect(x: 13, y: 16, width: 24, height: 24))
            leftImageV.image = "Login_password".toImage
            let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 56, height: 50))
            leftView.addSubview(leftImageV)
            v.leftView = leftView
            v.leftViewMode = .always
            v.isSecureTextEntry = true
            v.delegate = self
        }
        
        newPasswordTextfield.style { v in
            v.placehold("#7B7D80".toColor, font: 14.font_medium)
            let leftImageV = UIImageView(frame: CGRect(x: 13, y: 16, width: 24, height: 24))
            leftImageV.image = "Login_password".toImage
            let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 56, height: 50))
            leftView.addSubview(leftImageV)
            v.leftView = leftView
            v.leftViewMode = .always
            v.isSecureTextEntry = true
            v.delegate = self
        }
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        sureButton.addTarget(self, action: #selector(clickSureButtonButton), for: .touchUpInside)
        sureButton.isEnabled = false
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        updateSureButtonState()
        return true
    }
    
    private func updateSureButtonState(){
        if oldPasswordTextfield.text?.count ?? 0 > 0 && newPasswordTextfield.text?.count ?? 0 > 0 {
            sureButton.isEnabled = true
        }else{
            sureButton.isEnabled = false
        }
    }
    
    @objc private func clickSureButtonButton(){
        
        guard let oldPass = oldPasswordTextfield.text, oldPass.validatePassword() else {
            "请输入正确的旧密码".toast_error
            return
        }
        guard let password = newPasswordTextfield.text, password.validatePassword() else {
            "请输入正确的新密码".toast_error
            return
        }
        
        let dict = ["oldpasswd": oldPass, "passwd": password, "repasswd": password]
        Task{
            ToastUtil.show()
            let models = await network.requestSpecial(.resetPassword(json: dict))
            ToastUtil.dismiss()
            if models.isSuccess {
                UserDefaultsManager.clearUserDefaults()
                self.navigationController?.popViewController(animated: true)
            }else{
                models.errorMessage?.toast_error
            }
        }
    }
}
